package com.meitianhui.convenitenbuy.module.wallet.presenter;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.meitianhui.convenitenbuy.App;
import com.meitianhui.convenitenbuy.Constants;
import com.meitianhui.convenitenbuy.base.BaseListResponse;
import com.meitianhui.convenitenbuy.base.BaseResponse;
import com.meitianhui.convenitenbuy.config.InfoProvider;
import com.meitianhui.convenitenbuy.http.ResponseObserver;
import com.meitianhui.convenitenbuy.main.model.bean.UserToken;
import com.meitianhui.convenitenbuy.module.order.model.bean.EmptyInfo;
import com.meitianhui.convenitenbuy.module.wallet.contract.WalletScanningContract;
import com.meitianhui.convenitenbuy.module.wallet.http.WalletApiDataSource;
import com.meitianhui.convenitenbuy.module.wallet.model.ScanningBean;
import com.meitianhui.convenitenbuy.module.wallet.model.StoreInfoBean;
import com.meitianhui.convenitenbuy.module.wallet.ui.activity.WalletScanningActivity;

import java.util.HashMap;
import java.util.Map;


/**
 * Created by Administrator on 2017/11/2.
 */

public class WalletScanningPresenter implements WalletScanningContract.Presenter {

    private WalletScanningActivity context;
    private final WalletScanningContract.View view;
    private Gson gson;

    public WalletScanningPresenter(WalletScanningActivity view,
                                   WalletScanningActivity context) {
        this.view = view;
        this.context = context;
        gson = new Gson();
    }

    @Override
    public void attachView(Object view) {
    }

    @Override
    public void detachView() {
    }

    @Override
    public void verifyCode(ScanningBean bean) {
        String appToken = InfoProvider.getAppToken(App.getInstance());
        String flowNo = bean.getFlow_no();
        String securityCode = bean.getSecurity_code();

        WalletApiDataSource.getInstance()
                .verifyCode(appToken, flowNo, securityCode)
                .subscribe(new ResponseObserver<BaseListResponse<EmptyInfo>>(App.getInstance()) {
                    @Override
                    public void onSuccess(BaseListResponse<EmptyInfo> response) {
                        view.verifyCodeResult(1, bean, null);
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        switch (code) {
                            case Constants.FAILED:
                                view.verifyCodeResult(0, null, msg);
                                break;
                            case Constants.FAILED_No_NET:
                                view.verifyCodeResult(-1, null, msg);
                                break;
                        }
                    }
                });
    }

    @Override
    public void getStore(String storeId) {
        String appToken = InfoProvider.getAppToken(App.getInstance());
        String service = "member.storeDetailForConsumerFind";

        Map<String, String> map = new HashMap<>();
        map.put("stores_id", storeId);
        UserToken token = InfoProvider.getUserToken(App.getInstance());
        map.put("consumer_id", token.getMember_id());
        String jsonParams = gson.toJson(map);

        WalletApiDataSource.getInstance()
                .getStore(appToken, service, jsonParams)
                .subscribe(new ResponseObserver<BaseResponse<StoreInfoBean>>(App.getInstance()) {
                    @Override
                    public void onSuccess(BaseResponse<StoreInfoBean> response) {
                        view.getStoreBack(response.data, storeId);
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        super.onFail(code, msg);
                        view.verifyCodeResult(0, null, null);
                    }
                });
    }

    @Override
    public void payForOrder(ScanningBean bean) {
        String detail = TextUtils.equals(bean.getReceive_type(), "H") ? "零钱支付" : "扫码支付";
        UserToken token = InfoProvider.getUserToken(App.getInstance());
        String userToken = token.getUser_token();
        String service = "finance.balancePay";

        Map<String, String> params = new HashMap<>();
        params.put("data_source", Constants.CUSTOM_APP);
        params.put("detail", detail);
        params.put("amount", bean.getAmount());
        params.put("out_trade_no", bean.getFlow_no());
        params.put("payment_way_key", Constants.PAYMENT_WAY_05);
        params.put("buyer_id", token.getMember_id());
        params.put("seller_id", bean.getMember_id());

        Map<String, String> params2 = new HashMap<>();
        params2.put("security_code", bean.getSecurity_code());
        params2.put("security_code_type", "R");
        params2.put("buyer_id", token.getMember_id());
        params2.put("seller_id", bean.getMember_id());
        params.put("out_trade_body", gson.toJson(params2));
        String jsonParams = gson.toJson(params);

        WalletApiDataSource.getInstance()
                .payForOrder(userToken, service, jsonParams)
                .subscribe(new ResponseObserver<BaseResponse<EmptyInfo>>(App.getInstance()) {
                    @Override
                    public void onSuccess(BaseResponse<EmptyInfo> response) {
                        view.paymentResult(true, null);
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        super.onFail(code, msg);
                        view.paymentResult(false, msg);
                    }
                });
    }

    @Override
    public void receiptByOrder(ScanningBean bean) {
        UserToken token = InfoProvider.getUserToken(App.getInstance());
        String userToken = token.getUser_token();
        String service = "finance.balancePay";

        Map<String, String> params = new HashMap<>();
        params.put("data_source", Constants.CUSTOM_APP);
        params.put("detail", "转账");
        params.put("payment_way_key", Constants.PAYMENT_WAY_05);
        params.put("amount", bean.getAmount());
        params.put("out_trade_no", bean.getFlow_no());
        params.put("buyer_id", bean.getMember_id());
        params.put("seller_id", token.getMember_id());

        Map<String, String> params2 = new HashMap<>();
        params2.put("security_code_type", "P");
        params2.put("security_code", bean.getSecurity_code());
        params2.put("buyer_id", bean.getMember_id());
        params2.put("seller_id", token.getMember_id());
        params.put("out_trade_body", gson.toJson(params2));
        String jsonParams = gson.toJson(params);

        WalletApiDataSource.getInstance()
                .receivePay(userToken, service, jsonParams)
                .subscribe(new ResponseObserver<BaseResponse<EmptyInfo>>(App.getInstance()) {
                    @Override
                    public void onSuccess(BaseResponse<EmptyInfo> response) {
                        view.receiptResult(true, null);
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        super.onFail(code, msg);
                        view.receiptResult(false, msg);
                    }
                });
    }

}
